package org.example.java.util.colls;

import java.util.HashMap;

/**
 * User: ZhangJuntao
 * Date: 14-2-13
 * Time: 下午5:39
 */
public class HashMapTest {
  public static void main(String[] args) {
    class MyKey {
      Integer i;

      public MyKey(Integer i) {
        this.i = i;
      }

      @Override
      public int hashCode() {
        return 1;
      }

      @Override
      public boolean equals(Object obj) {
        return ( obj).equals(i);
      }

      @Override
      public String toString() {
        final StringBuilder sb = new StringBuilder("MyKey{");
        sb.append("i=").append(i);
        sb.append('}');
        return sb.toString();
      }
    }
    HashMap<MyKey, String> map = new HashMap<MyKey, String>();
    for (int i = 0; i < 65; i++) {
      String str = String.valueOf(i);
      MyKey k = new MyKey(i);
      map.put(k, str);
    }
//    map.put("a", "1.1");
//    map.put("a", "1.2");
//    map.put(null, "1");
//    map.get("a");
//    map.remove("a");
    System.out.println(hash(Integer.MAX_VALUE));
    System.out.println(hash(10000));
  }

  private static String toBinaryString(int num) {
    String str = Integer.toBinaryString(num);
    int i = 32 - str.length();
    while (i-- > 0) str = "0" + str;
    System.out.println(str);
    return str;
  }

  static int hash(int h) {
    // This function ensures that hashCodes that differ only by
    // constant multiples at each bit position have a bounded
    // number of collisions (approximately 8 at default load factor).
    toBinaryString(h);
    toBinaryString(h >>> 20);
    toBinaryString(h >>> 12);
    h ^= (h >>> 20) ^ (h >>> 12);
    toBinaryString(h);
    int tmp = h ^ (h >>> 7) ^ (h >>> 4);
    System.out.println();
    toBinaryString(h);
    toBinaryString(h >>> 7);
    toBinaryString(h >>> 4);
    toBinaryString(tmp);
    System.out.println();
    return tmp;
  }

}
